


local rect = ac.rect.j_rect('CD2')
local td = rect:get_point()
local mt = ac.skill['团队出怪']
mt.level = 0
mt.max_level = 30
mt.attack = td - {0,2000}
mt.base = td
mt.units = {
    '存档小怪1号',
    '存档小怪2号',
    '存档小怪3号',
}
mt.boss = {
    '[装备]巫妖',
    '[装备]恐惧魔王',
    '[装备]地穴领主',
}

function mt:add_enemy()
    self.level = self.level + 1
    if self.timer then
        self.timer:remove()
    end
    if self.level>self.max_level then
        return
    end
    
    if self.level%10==0 then
        local level = self.level
        local name = self.boss[self.level/10]
        local unit = ac.player[12]:create_unit(name,self.attack)
        self.group[unit.handle] = unit
        unit:set_enemy_level(unit['默认等级'])
        unit:issue_order('attack',self.base)
        unit:event '单位-死亡'(function()
            ac.game:notify('游戏-获得装备存档')
            if level==30 then
                self:remove()
            end
        end)

    end
    self.timer = ac.loop(200,function()
        local name = table.random(self.units)
        local unit = ac.player[12]:create_unit(name,self.attack - {math.random(360),math.random(500)})
        self.group[unit.handle] = unit
        unit:set_enemy_level(unit['默认等级'])
        unit:add('基础攻击%',self.level*10)
        unit:add('生命上限%',self.level*10)
        unit:issue_order('attack',self.base)
        unit_add_life2(unit)
    end)
    self.timer:on_timer()
    
    self.title = ('难%s | 第%s/%s波'):format(self.hard,self.level,self.max_level)
    ac.game:notify('游戏-设置顶部计时',self.title,self.timer,'下一波：')
end

function mt:on_add()
    local base = self.owner
    self.hard = ac.enemy and ac.enemy.hard or 1
    self.group = {}
    base:set('生命上限',50)
    base:set('生命恢复',0.5)
    local timer = ac.loop(5*1000,function()
        self:add_enemy()
    end)
    timer:on_timer()
    self:gc(timer)
    self:gc(base:event '单位-即将受到伤害'(function(_,_,target,data)
        if data:is_attack() then
            data.current_damage = 1
            if target:is_type('精英') then
                data.current_damage = 3
            end
            if target:is_type('Boss') then
                data.current_damage = 10
            end
            ac.player.self:send_msg('|cffff0000基地正受到攻击!!!!|r')
        else
            data.current_damage = 0
        end
    end))
    self:gc(base:event '单位-死亡'(function()
        self:remove()
    end))
    self:gc(function()
        base:remove()
        if self.timer then
            self.timer:remove()
        end
        -- for _,unit in pairs(self.group) do
        --     unit:remove()
        -- end
        ac.game:notify('结束团队试炼',self.level)
    end)
end


ac.game:event '开启团队试炼'(function(trg)
    trg:remove()
    local base = ac.player[5]:create_unit('基地',td)
    unit_add_life2(base,1.5)
    base:add_skill('团队出怪')
    ac.each_player(function(player)
        local point = player.point1
        player:notify('玩家-传送',player,td)
        player.point1 = td
        player.save_cd = point
    end)
end)

ac.game:event '结束团队试炼'(function(trg,flag)
    trg:remove()
    ac.each_player(function(player)
        local point = player.save_cd
        player.point1 = td
    end)
    if flag then

    end
end)


ac.game:event '单位'
